package com;

import java.util.Collections;
import java.util.List;

import org.junit.*;
import org.openqa.selenium.*;
import static org.junit.Assert.*;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.Select;

import pages.InternalPage;
import pages.LoginPage;
import pages.AnyPage;
import pages.UserManagmentPage;

public class CreateNewUserTest extends TestBase {

	private UserManagmentPage userManagmentPage;
	private AnyPage anyPage;
	private LoginPage loginPage;
	private InternalPage internalPage;

	boolean testUserNameAlreadyExist;
	String newUserEmail = "Tony.Montano@gmail.com";

	@Test
	public void createNweUserTest() throws Exception {
		goToPage("http://localhost/php4dvd/");
		loginTest("admin", "admin");

		goToUserManagmentPage();
		checkTestUserAlredyExist("Tony.Montano@gmail.com");
		wait(200);

		if (testUserNameAlreadyExist) {
			deleteUser("Tony.Montano@gmail.com");
			
			List <Users> oldUserList = userManagmentPage.getUsers();
			
			Users user = new Users().withName("Tony").withEmail("Tony.Montano@gmail.com").withPassword("dfjH%(;R@sa");
			
			feelTestUserCreatinForm(user);
			choosUserRole("Editor");			
			clickSubmitButtonOnuserManagmentPage();
			wait(200);
			List <Users> newUserList = userManagmentPage.getUsers();
			
			oldUserList.add(user);	
			Collections.sort(oldUserList);
			Collections.sort(newUserList);
			assertEquals(oldUserList, newUserList);
			} 
		else {
			List <Users> oldUserList = userManagmentPage.getUsers();			
			Users user = new Users().withName("Tony").withEmail("Tony.Montano@gmail.com").withPassword("dfjH%(;R@sa");
			
			feelTestUserCreatinForm(user);
			choosUserRole("Editor");
			clickSubmitButtonOnuserManagmentPage();
			
			List <Users> newUserList = userManagmentPage.getUsers();
			
			oldUserList.add(user);	
			Collections.sort(oldUserList);
			Collections.sort(newUserList);
			assertEquals(oldUserList, newUserList);  
		}

	}
	
	

	private void choosUserRole(String userRole) {
		Select select = new Select(userManagmentPage.permissionField);
		select.selectByVisibleText(userRole);
	}

	private boolean checkTestUserAlredyExist(String userName) {
		String s = anyPage.htmlBodyXpath.getText();
		testUserNameAlreadyExist = s.contains(userName);
		return testUserNameAlreadyExist;
	}

	private void deleteUser(String userEmail) {
		driver.findElement(By.xpath("//tr[contains(.,'" + newUserEmail + "')]")).findElement(By.cssSelector("img[alt=\"Remove\"]")).click();
		driver.switchTo().alert().accept();
	}

	private void feelTestUserCreatinForm(Users user) {

		userManagmentPage.userNameField.clear();
		userManagmentPage.userNameField.sendKeys(user.name);
		userManagmentPage.emailField.clear();
		userManagmentPage.emailField.sendKeys(user.email);
		userManagmentPage.passwordField.clear();
		userManagmentPage.passwordField.sendKeys(user.password);
		userManagmentPage.password2Field.clear();
		userManagmentPage.password2Field.sendKeys(user.password);		
	}
	
	private void clickSubmitButtonOnuserManagmentPage (){
		userManagmentPage.submitButton.click();
	}
	

	private void loginTest(String userName, String password) {
		loginPage.userNameFieldOnLoginPage.sendKeys(userName);
		loginPage.passwordField.clear();
		loginPage.passwordField.sendKeys(password);
		loginPage.loginButton.click();
	}

	private void goToPage(String url) {
		driver.get(url);
		loginPage = PageFactory.initElements(driver, LoginPage.class);
		internalPage = PageFactory.initElements(driver, InternalPage.class);
		anyPage = PageFactory.initElements(driver, AnyPage.class);
		userManagmentPage = PageFactory.initElements(driver, UserManagmentPage.class);
	}

	private void goToUserManagmentPage() {
		internalPage.userManagmentLink.click();
	}

}
